package kd.hc.utils;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class BusinessDataServiceHelperExt extends BusinessDataServiceHelper {

    public static DynamicObject loadSingleByNumber(String number, String entityNumber) {
        QFilter[] qFilters = {new QFilter("number", QCP.equals, number)};
        return BusinessDataServiceHelper.loadSingle(entityNumber, qFilters);
    }

    public static DynamicObject loadSingleByOneCondition(String entityNumber, String key, String value) {
        QFilter[] qFilters = {new QFilter(key, QCP.equals, value)};
        return BusinessDataServiceHelper.loadSingle(entityNumber, qFilters);
    }

    public static DynamicObject[] loadDyListOnlyIdOneCondition(String entityNumber, String key, Object value) {
        QFilter[] qFilters = {new QFilter(key, QCP.equals, value)};
        return BusinessDataServiceHelper.load(entityNumber, "id", qFilters);
    }

    public static DynamicObject[] loadDyListOneCondition(String entityNumber, String key, Object value) {
        if (StringUtils.isBlank(entityNumber) || StringUtils.isBlank(key) || value == null) {
            throw new KDBizException("该方法参数异常!");
        }
        MainEntityType entityType = EntityMetadataCache.getDataEntityType(entityNumber);
        DynamicObject[] ids = loadDyListOnlyIdOneCondition(entityNumber, key, value);
        List<DynamicObject> idsList = Arrays.stream(ids).collect(Collectors.toList());
        Object[] pks = idsList.stream().map(m1 -> m1.getPkValue()).collect(Collectors.toList()).toArray();
        return BusinessDataServiceHelper.load(pks, entityType);
    }


    public static DynamicObject loadSingleByBillno(String number, String entityNumber) {
        QFilter[] qFilters = {new QFilter("billno", QCP.equals, number)};
        return BusinessDataServiceHelper.loadSingle(entityNumber, qFilters);
    }

    public static DynamicObject loadCurrency(String number) {
        QFilter[] qFilters = {new QFilter("number", QCP.equals, number)};
        return BusinessDataServiceHelper.loadSingle("bd_currency", qFilters);
    }

    public static DynamicObject loadCurrencyCNY() {
        QFilter[] qFilters = {new QFilter("number", QCP.equals, "CNY")};
        return BusinessDataServiceHelper.loadSingle("bd_currency", qFilters);
    }

    public static DynamicObject loadAssistantData(Object pk) {
        return BusinessDataServiceHelper.loadSingle(pk, "bos_assistantdata_detail");
    }

    public static DynamicObject loadAssistantData(DynamicObject data) {
        if (data != null) {
            return BusinessDataServiceHelper.loadSingle(data.getPkValue(), "bos_assistantdata_detail");
        } else {
            return null;
        }

    }

    public static DynamicObject loadAssistantDataByObject(Object data) {
        if (data != null) {
            return BusinessDataServiceHelper.loadSingle(((DynamicObject)data).getPkValue(), "bos_assistantdata_detail");
        } else {
            return null;
        }

    }

}
